package com.yx.test3d.model;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.Matrix;
import android.util.Log;


import com.yx.test3d.utils.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class Cube extends Model {

/*    private   int LOCATION_VERTEX ;
    private   int LOCATION_COLOR ;
    private   int LOCATION_MATRIX_M ;
    private   int LOCATION_MATRIX_V;
    private   int LOCATION_MATRIX_P;
    private static final int VERTEX_SIZE = 3;
    private static final int COLOR_SIZE = 3;
    protected FloatBuffer vertexBuffer;
    protected FloatBuffer colorBuffer;
    private IntBuffer indexBuffer;

    public Cube(float[] vertex, int[] indexs, float[] color) {
        setVertex(vertex);
        setVertexColor(color);
        setVertexIndex(indexs);
    }

    public void setVertex(float[] vertex) {
        vertexBuffer = ByteBuffer.allocateDirect(vertex.length * FLOAT_SIZE)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer().put(vertex);
        vertexBuffer.flip();
    }

    public void setVertexColor(float[] color) {
        colorBuffer = ByteBuffer.allocateDirect(color.length * FLOAT_SIZE)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer().put(color);
        colorBuffer.flip();
    }


    public void setVertexIndex(int[] index) {
        indexBuffer = ByteBuffer.allocateDirect(index.length * FLOAT_SIZE)
                .order(ByteOrder.nativeOrder())
                .asIntBuffer().put(index);
        indexBuffer.flip();
    }

    @Override
    public void setMatrix(float[] mMatrix, float[] vMatrix, float[] pMatrix) {
        super.setMatrix(mMatrix,vMatrix,pMatrix);
    }

    @Override
    public void onCreate(Context context) {
        vertexShaderCode = ShaderUtil.loadFromAssetsFile("shader/triangle.vert", context.getResources());
        fragmentShaderCode = ShaderUtil.loadFromAssetsFile("shader/triangle.frag", context.getResources());
        programId = ShaderUtil.createProgram(vertexShaderCode, fragmentShaderCode);
        initLocation();
    }
    private void initLocation(){
        LOCATION_VERTEX = GLES30.glGetAttribLocation(programId, "position");
        LOCATION_COLOR = GLES30.glGetAttribLocation(programId, "color");
        LOCATION_MATRIX_P = GLES30.glGetUniformLocation(programId, "projection");
        LOCATION_MATRIX_V = GLES30.glGetUniformLocation(programId, "view");
        LOCATION_MATRIX_M = GLES30.glGetUniformLocation(programId, "model");
    }

    @Override
    public void onDraw() {
        Log.i("cube TAG", "onDrawFrame cube programId: " + programId);
        if (programId == 0) {
            return;
        }
        GLES30.glUseProgram(programId);
        GLES30.glEnableVertexAttribArray(LOCATION_VERTEX);
        GLES30.glEnableVertexAttribArray(LOCATION_COLOR);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_M, 1, false, mMatrix, 0);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_V, 1, false, vMatrix, 0);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_P, 1, false, pMatrix, 0);

        GLES30.glVertexAttribPointer(LOCATION_VERTEX, VERTEX_SIZE, GLES30.GL_FLOAT, false, FLOAT_SIZE * VERTEX_SIZE, vertexBuffer);
        GLES30.glVertexAttribPointer(LOCATION_COLOR, COLOR_SIZE, GLES30.GL_FLOAT, false, FLOAT_SIZE * COLOR_SIZE, colorBuffer);

        GLES30.glDrawElements(GLES30.GL_TRIANGLES, indexBuffer.capacity(), GLES30.GL_UNSIGNED_INT, indexBuffer);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vertexBuffer.capacity() );

        GLES30.glDisableVertexAttribArray(LOCATION_VERTEX);
        GLES30.glDisableVertexAttribArray(LOCATION_COLOR);
    }

    @Override
    public void onDestroy() {
        vertexBuffer.clear();
        colorBuffer.clear();
    }*/

}
